package top.aylan.org.service.impl;

import org.springframework.stereotype.Service;
import top.aylan.org.dao.BannerMapper;
import top.aylan.org.entity.Banner;
import top.aylan.org.entity.BannerExample;
import top.aylan.org.service.BannerService;
import top.aylan.org.tools.Pager;

import javax.annotation.Resource;
import java.util.List;
@Service
public class BannerServiceImpl implements BannerService {

    @Resource
    BannerMapper bannerMapper;

    @Override
    public List<Banner> selectBannerInUse() throws Exception {
        BannerExample example = new BannerExample();
        BannerExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo((byte)1);
        return bannerMapper.selectByExample(example);
    }

    @Override
    public List<Banner> selectBannerByPage(Pager pager) throws Exception {
        BannerExample example = new BannerExample();
        BannerExample.Criteria criteria = example.createCriteria();
        pager.setTotalRow(bannerMapper.countByExample(example));
        example.setOffset((pager.getCurrentPage() - 1)*pager.getEveyPageRow());
        example.setSize(pager.getEveyPageRow());
        return bannerMapper.selectByExampleWithPage(example);
    }

    @Override
    public int getCount(BannerExample example) throws Exception {
        return bannerMapper.countByExample(example);
    }

    @Override
    public Banner findById(Integer id) throws Exception {
        return bannerMapper.selectByPrimaryKey(id);
    }

    @Override
    public Banner findByExample(BannerExample example) throws Exception {
        return null;
    }

    @Override
    public int delete(Integer id) throws Exception {
        return bannerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int save(Banner banner) throws Exception {
        return bannerMapper.insertSelective(banner);
    }

    @Override
    public int updateById(Integer id, Banner banner) throws Exception {
        banner.setId(id);
        return bannerMapper.updateByPrimaryKeySelective(banner);
    }
}
